import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";

var _Symbol$iterator;

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _class;

/**
 * customize - 客制化 配置
 * TODO: config.lock - 一些数据 是不能修改的
 * @date 2019/9/18
 */
import EventEmitter from 'event-emitter';
import { Bind } from 'lodash-decorators';
var GlobalMapStore = (_dec = Bind(), _dec2 = Bind(), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), _dec7 = Bind(), _dec8 = Bind(), _dec9 = Bind(), _dec10 = Bind(), (_class = (_Symbol$iterator = Symbol.iterator, /*#__PURE__*/function () {
  function GlobalMapStore() {
    var _this = this;

    _classCallCheck(this, GlobalMapStore);

    this.moduleMapStoreMap = void 0;
    this.eventManager = void 0;

    this[_Symbol$iterator] = function (_ref) {
      var module = _ref.module,
          feature = _ref.feature;
      var featureMap;

      var featureStore = _this.moduleMapStoreMap.get(module);

      if (featureStore) {
        featureMap = featureStore.get(feature);

        if (!featureMap) {
          featureMap = new Map();
          featureStore.set(feature, featureMap);
        }
      } else {
        featureStore = new Map();

        _this.moduleMapStoreMap.set(module, featureStore);

        featureMap = new Map();
        featureStore.set(feature, featureMap);
      }

      return featureMap[Symbol.iterator]();
    };

    this.moduleMapStoreMap = new Map();
    this.eventManager = EventEmitter();
  }

  _createClass(GlobalMapStore, [{
    key: "emit",
    value: function emit(type) {
      var _this$eventManager;

      for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
        args[_key - 1] = arguments[_key];
      }

      return (_this$eventManager = this.eventManager).emit.apply(_this$eventManager, [type].concat(args));
    }
  }, {
    key: "on",
    value: function on(type, listener) {
      return this.eventManager.on(type, listener);
    }
  }, {
    key: "once",
    value: function once(type, listener) {
      return this.eventManager.once(type, listener);
    }
  }, {
    key: "off",
    value: function off(type, listener) {
      return this.eventManager.off(type, listener);
    }
    /**
     * 清空对应的 数据
     * @param {String} module - 模块名
     * @param {String} feature - 功能名
     * @param {any} [...config] - 预留配置
     */

  }, {
    key: "clear",
    value: function clear(_ref2) {
      var module = _ref2.module,
          feature = _ref2.feature;
      var featureStore = this.moduleMapStoreMap.get(module);

      if (featureStore) {
        var featureMap = featureStore.get(feature);

        if (featureMap) {
          featureMap.clear();
        }
      }
    }
    /**
     * 删除对应 storeMap key 的 entry
     * 删除成功 返回 true
     * 未设置 返回 false
     * @param {String} module - 模块名
     * @param {String} feature - 功能名
     * @param {String} key - map 的 key
     * @param {any} [...config] - 预留配置
     */

  }, {
    key: "delete",
    value: function _delete(_ref3) {
      var module = _ref3.module,
          feature = _ref3.feature,
          key = _ref3.key;
      var featureStore = this.moduleMapStoreMap.get(module);

      if (featureStore) {
        var storeMap = featureStore.get(feature);

        if (storeMap) {
          return storeMap.delete(key);
        }
      }

      return false;
    }
    /**
     * 返回 对应 数据的 entries
     * @param {String} module - 模块名
     * @param {String} feature - 功能名
     * @param {any} [...config] - 预留配置
     */

  }, {
    key: "entries",
    value: function entries(_ref4) {
      var module = _ref4.module,
          feature = _ref4.feature;
      var mapStore;
      var featureStore = this.moduleMapStoreMap.get(module);

      if (featureStore) {
        mapStore = featureStore.get(feature);

        if (!mapStore) {
          mapStore = new Map();
          featureStore.set(feature, mapStore);
        }
      } else {
        featureStore = new Map();
        this.moduleMapStoreMap.set(module, featureStore);
        mapStore = new Map();
        featureStore.set(feature, mapStore);
      }

      return mapStore.entries();
    }
    /**
     * 遍历对应的 数据
     * @param {String} module - 模块名
     * @param {String} feature - 功能名
     * @param {Function} fn - 遍历方法 (value, key, map) => void
     * @param {?any} thisArg - 遍历方法对应的 this
     * @param {any} [...config] - 预留配置
     */

  }, {
    key: "forEach",
    value: function forEach(_ref5) {
      var module = _ref5.module,
          feature = _ref5.feature,
          fn = _ref5.fn,
          thisArg = _ref5.thisArg;
      var featureStore = this.moduleMapStoreMap.get(module);

      if (featureStore) {
        var featureMap = featureStore.get(feature);

        if (featureMap) {
          featureMap.forEach(fn, thisArg);
        }
      }
    }
    /**
     * 获取数据
     * 获取到 返回 数据
     * 否则 返回 undefined
     * @param {String} module - 模块名
     * @param {String} feature - 功能名
     * @param {String} key - map 的 key
     * @param {any} [...config] - 预留配置
     * @return {any|undefined}
     */

  }, {
    key: "get",
    value: function get(_ref6) {
      var module = _ref6.module,
          feature = _ref6.feature,
          key = _ref6.key;
      var featureStore = this.moduleMapStoreMap.get(module);

      if (featureStore) {
        var featureMap = featureStore.get(feature);

        if (featureMap) {
          if (featureMap.has(key)) {
            return featureMap.get(key);
          }
        }
      }

      return undefined;
    }
    /**
     * 是否存在 数据
     * 存在 返回 true
     * 不存在/未设置 返回 false
     * @param {String} module - 模块名
     * @param {String} feature - 功能名
     * @param {String} key - map 的 key
     * @param {any} [...config] - 预留配置
     */

  }, {
    key: "has",
    value: function has(_ref7) {
      var module = _ref7.module,
          feature = _ref7.feature,
          key = _ref7.key;
      var featureStore = this.moduleMapStoreMap.get(module);

      if (featureStore) {
        var featureMap = featureStore.get(feature);

        if (featureMap) {
          return featureMap.has(key);
        }
      }

      return false;
    }
    /**
     * 返回 对应 数据的 keys
     * @param {String} module - 模块名
     * @param {String} feature - 功能名
     * @param {any} [...config] - 预留配置
     */

  }, {
    key: "keys",
    value: function keys(_ref8) {
      var module = _ref8.module,
          feature = _ref8.feature;
      var featureMap;
      var featureStore = this.moduleMapStoreMap.get(module);

      if (featureStore) {
        featureMap = featureStore.get(feature);

        if (!featureMap) {
          featureMap = new Map();
          featureStore.set(feature, featureMap);
        }
      } else {
        featureStore = new Map();
        this.moduleMapStoreMap.set(module, featureStore);
        featureMap = new Map();
        featureStore.set(feature, featureMap);
      }

      return featureMap.keys();
    }
    /**
     * 设置 entry 数据
     * @param {String} module - 模块名
     * @param {String} feature - 功能名
     * @param {String} key - map 的 key
     * @param {any} data - map key 对应的 value
     * @param {any} [...config] - 预留配置
     */

  }, {
    key: "set",
    value: function set(_ref9) {
      var module = _ref9.module,
          feature = _ref9.feature,
          key = _ref9.key,
          data = _ref9.data;
      var featureMap;
      var featureStore = this.moduleMapStoreMap.get(module);

      if (featureStore) {
        featureMap = featureStore.get(feature);

        if (!featureMap) {
          featureMap = new Map();
          featureStore.set(feature, featureMap);
        }
      } else {
        featureStore = new Map();
        this.moduleMapStoreMap.set(module, featureStore);
        featureMap = new Map();
        featureStore.set(feature, featureMap);
      }

      featureMap.set(key, data);
      this.emit('set', {
        module: module,
        feature: feature,
        key: key,
        data: data
      });
      return this;
    }
    /**
     * 返回 对应 数据的 values
     * @param {String} module - 模块名
     * @param {String} feature - 功能名
     * @param {any} [...config] - 预留配置
     */

  }, {
    key: "values",
    value: function values(_ref10) {
      var module = _ref10.module,
          feature = _ref10.feature;
      var featureMap;
      var featureStore = this.moduleMapStoreMap.get(module);

      if (featureStore) {
        if (!featureMap) {
          featureMap = new Map();
          featureStore.set(feature, featureMap);
        }
      } else {
        featureStore = new Map();
        this.moduleMapStoreMap.set(module, featureStore);
        featureMap = new Map();
        featureStore.set(feature, featureMap);
      }

      return featureMap.values();
    }
    /**
     * 获取对应数据的 @@iterator
     * @param {String} module - 模块名
     * @param {String} feature - 功能名
     * @param {any} [...config] - 预留配置
     */

  }, {
    key: "size",
    value:
    /**
     * 获取对应数据的 数量
     * 未设置 返回 0
     * 已设置 返回 数据 数量
     * @param {String} module - 模块名
     * @param {String} feature - 功能名
     * @param {any} [...config] - 预留配置
     */
    function size(_ref11) {
      var module = _ref11.module,
          feature = _ref11.feature;
      var featureStore = this.moduleMapStoreMap.get(module);

      if (featureStore) {
        var featureMap = featureStore.get(feature);

        if (featureMap) {
          return featureMap.size;
        }
      }

      return 0;
    }
  }]);

  return GlobalMapStore;
}()), (_applyDecoratedDescriptor(_class.prototype, "clear", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "clear"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "delete", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "delete"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "entries", [_dec3], Object.getOwnPropertyDescriptor(_class.prototype, "entries"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "forEach", [_dec4], Object.getOwnPropertyDescriptor(_class.prototype, "forEach"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "get", [_dec5], Object.getOwnPropertyDescriptor(_class.prototype, "get"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "has", [_dec6], Object.getOwnPropertyDescriptor(_class.prototype, "has"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "keys", [_dec7], Object.getOwnPropertyDescriptor(_class.prototype, "keys"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "set", [_dec8], Object.getOwnPropertyDescriptor(_class.prototype, "set"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "values", [_dec9], Object.getOwnPropertyDescriptor(_class.prototype, "values"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "size", [_dec10], Object.getOwnPropertyDescriptor(_class.prototype, "size"), _class.prototype)), _class));
var GLOGAL_CUSTOMIZE_STORE_KEY = 'GLOGAL_CUSTOMIZE_STORE_KEY';

var getSingleInstanceMapCustomize = function getSingleInstanceMapCustomize() {
  var singleInstance = window[GLOGAL_CUSTOMIZE_STORE_KEY];

  if (!singleInstance) {
    singleInstance = new GlobalMapStore();
    window[GLOGAL_CUSTOMIZE_STORE_KEY] = singleInstance;
  }

  return singleInstance;
}; // <globalMapStore>

/**
 * @param {String} module - 模块名
 * @param {String} feature - 功能名
 * @param {String} key - map 的 key
 * @param {any} [...config] - 预留配置
 */
// </globalMapStore>


var mapCustomize = getSingleInstanceMapCustomize();
export { mapCustomize };
export default mapCustomize;